package com.jungle.client.common;


import javafx.beans.InvalidationListener;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableColumn;

import java.util.function.Function;

public class MyObservableValue<S, T> implements ObservableValue<T> {
    private TreeTableColumn.CellDataFeatures<S, T> cellDataFeatures;
    private Function<S, T> valueMethod;

    public MyObservableValue(TreeTableColumn.CellDataFeatures<S, T> cellDataFeatures, Function<S, T> valueMethod) {
        this.cellDataFeatures = cellDataFeatures;
        this.valueMethod = valueMethod;
    }

    @Override
    public void addListener(ChangeListener<? super T> changeListener) {
    }

    @Override
    public void removeListener(ChangeListener<? super T> changeListener) {
    }

    @Override
    public T getValue() {
        if (cellDataFeatures == null) {
            return null;
        }
        TreeItem<S> treeItem = cellDataFeatures.getValue();
        if (treeItem == null) {
            return null;
        }
        S value = treeItem.getValue();
        return valueMethod.apply(treeItem.getValue());
    }

    @Override
    public void addListener(InvalidationListener invalidationListener) {
    }

    @Override
    public void removeListener(InvalidationListener invalidationListener) {
    }
}
